using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Uportal.Shp.Init.TJ;
using System.Collections.Generic;
using Uportal.Shp.Core.Common;

namespace Uportal.Shp.Init.Features.Uportal.Shp.TJ
{
    [Guid("7cd8c329-5cfd-497e-9308-5d3a8b4505f0")]
    public class UportalShpEventReceiver : SPFeatureReceiver
    {
        NLogLogger _logger = new NLogLogger(@"c:\Temp\Logs\Uportal.TJ_${shortdate}.log");

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent != null)
            {
                try
                {
                    CreateTJ(properties);
                }
                catch (Exception ex)
                {
                   _logger.Error(ex.Message);
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent != null)
            {
                try
                {
                    DeleteTJ(properties);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                }
            }
        }


        private void CreateTJ(SPFeatureReceiverProperties prop)
        {
            string siteUrl = ((SPWeb)prop.Feature.Parent).Site.Url;
            Guid webGuid = ((SPWeb)prop.Feature.Parent).ID;
            SPSecurity.RunWithElevatedPrivileges(()=>
            {
                using (var site = new SPSite(siteUrl))
                {
                    using (var web = site.OpenWeb(webGuid))
                    {
                        if (web == null)
                            return;

                        var collection = web.Site.WebApplication.JobDefinitions;
                        foreach (SPJobDefinition job in collection)
                        {
                            if (job.Name == "Uportal.ReminderFeedBack")
                                job.Delete();
                        }
                        
                        _logger.Info("SiteId:" + web.Site.ID.ToString());
                        _logger.Info("WebId:" + web.ID.ToString());

                        var sfdJob = new ReminderFeedBackTJ("Uportal.ReminderFeedBack", web.Site.WebApplication);
                        sfdJob.Properties["SiteId"] = web.Site.ID.ToString();
                        sfdJob.Properties["WebId"] = web.ID.ToString();
                        sfdJob.Update();
                    }
                }
            });
        }

        private void DeleteTJ(SPFeatureReceiverProperties prop)
        {
            using (var web = prop.Feature.Parent as SPWeb)
            {
                if (web == null)
                    return;
                var collection = web.Site.WebApplication.JobDefinitions;
                foreach (SPJobDefinition job in collection)
                {
                    if (job.Name == "Uportal.ReminderFeedBack")
                        job.Delete();
                }
            }
        }
    }
}